module.exports = (cb, perpage) => {
    // 每页记录条数得默认值为10
    perpage = perpage || 10;
    // 返回中间件函数
    return (req, res, next) => {
        // 将参数page解析为十进制得整型值
        let page = Math.max(parseInt(req.params.page || '1,10'), 1) - 1;
        // 调用传入得函数
        cb((err, total) => {
            // 传递错误
            if (err) return next(err);
            req.page = res.locals.page = {
                    number: page,
                    perpage: perpage,
                    from: page * perpage,
                    to: page * perpage + perpage - 1,
                    total: total,
                    count: Math.ceil(total / perpage)
                }
                // 将控制权交给下一个中间件
            next();
        })
    }
}